﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Data;
using System.Web.UI.WebControls;
using System.Web;

namespace WISD.Common
{
    public class Excel
    {
        /// <summary>
        /// 导出Excel（使用HtmlTextWriter + GridView）
        /// </summary>        
        /// <param name="fileName">文件名</param>
        public static void ExportExcel(IEnumerable source, string fileName)
        {
            GridView gv = new GridView();
            gv.DataSource = source;
            gv.DataBind();
            GridViewToExcel(gv, fileName);
        }

        /// <summary>
        /// 导出Excel（使用HtmlTextWriter + GridView）
        /// </summary>        
        /// <param name="fileName">文件名</param>
        public static void ExportExcel(DataTable source, string fileName)
        {
            GridView gv = new GridView();
            gv.DataSource = source;
            gv.DataBind();
            GridViewToExcel(gv, fileName);
        }

        internal static void GridViewToExcel(GridView gv, string fileName)
        {
            //为了防止类似"000123"被Excel刷成"123"
            //或者"32058119302931233"被刷成"3.20581...E17"之类的DT事
            foreach (GridViewRow row in gv.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    foreach (TableCell cell in row.Cells)
                    {
                        cell.Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                    }
                }
            }

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.BufferOutput = true;
            //设定输出的字符集
            HttpContext.Current.Response.Charset = "UTF-8";
            fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8).ToString();
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            ////设置导出文件的格式
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("ZH-CN", true);
            System.IO.StringWriter stringWriter = new System.IO.StringWriter(cultureInfo);
            System.Web.UI.HtmlTextWriter textWriter = new System.Web.UI.HtmlTextWriter(stringWriter);
            gv.RenderControl(textWriter);

            //在头上输这么一段据说是解决乱码问题的终级方案…………啊啊啊，头疼
            HttpContext.Current.Response.Write("<html><head><meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">");
            HttpContext.Current.Response.Write(stringWriter.ToString());
            HttpContext.Current.Response.Write("</body></html>");
            //HttpContext.Current.Response.End();
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }

    }
    public static class ClassExtentions
    {
        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <param name="source"></param>
        /// <param name="fileName"></param>
        public static void ToExcel(this IEnumerable source, string fileName)
        {
            Excel.ExportExcel(source, fileName);
        }


        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <param name="source"></param>
        /// <param name="fileName"></param>
        //public static void ToExcel(this object source, IEnumerable<DataControlField> boundFields, string fileName)
        //{
        //    GridControl gc = new GridControl();            
        //    gc.DataSource = source;

        //    foreach (var item in boundFields)
        //    {
        //        gc.Columns.Add(item);
        //    }
        //    gc.Refresh();
        //    gc.DataBind();
        //    Excel.GridViewToExcel(gc.BaseGrid, fileName);
        //}

        //public static void ToExcel(this GridControl gv, string fileName)
        //{
        //    Excel.GridViewToExcel(gv.BaseGrid, fileName);
        //}
    }
}
